﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebBanThuoc.Function;
using WebBanThuoc.Server;
using WebBanThuoc.DAO;
using WebBanThuoc.DTO;

namespace WebBanThuoc.admin
{
    public partial class QLChiTietDonHang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!FuncAdmin.IsLogin(Session))
            {
                Session.Add("URLCurrentActive_SendTo_Login", Request.RawUrl);
                Response.Redirect("/admin/DangNhap.aspx");
            }

            if (!IsPostBack)
            {

            }
            else
            {
                if (Request.Params["btn_Tim"] != null)
                {
                    getData();
                }
                if (Request.Params["btn_Them"] != null)
                {
                    getData_Add();
                }
            }
        }

        private void getData()
        {
            string MaSanPham = Request.Params["MaDonHang"];
            if (MaSanPham != null && MaSanPham.Trim() != "")
            {
                IServerWeb sw = new ServerWeb();
                DonHang_DAO daoDH = new DonHang_DAO(sw.Connection);
                DonHang dh = daoDH.getByKey("DH" + MaSanPham);
                if (dh != null)
                {
                    ChiTietDonHang_DAO daoCT = new ChiTietDonHang_DAO(sw.Connection);
                    List<ChiTietDonHang> dataCT = daoCT.getListByKey(dh.MaDonHang);
                    List<SanPham> dataSP = new List<SanPham>();
                    for (int i = 0; i < dataCT.Count; i++)
                    {
                        dataCT[i].GiamGia = dataCT[i].Gia * dataCT[i].SoLuong;
                        //SanPham_DAO daoSP = new SanPham_DAO(sw.Connection);
                        //SanPham sp = daoSP.getByKey(dataCT[i].MaSanPham);
                        //if (sp != null)
                        //{
                        //    dataSP.Add(sp);
                        //}
                        //else
                        //{
                        //    sp = new SanPham() { TenSanPham = "" };
                        //    dataSP.Add(sp);
                        //}
                    }

                    Session.Add("page_QLCTDonHang_dataDH", dh);
                    Session.Add("page_QLCTDonHang_dataCT", dataCT);
                    Session.Add("page_QLCTDonHang_dataSP", dataSP);

                    data_GridView.EditIndex = -1;
                    Bind_Data();

                    //CachDung.Text = sp.CachDung;
                    ////ThanhPhan.Text = sp.ThanhPhan;
                    //CongDung.Text = sp.CongDung;
                }
                else
                {
                    Session.Add("page_QLCTDonHang_dataDH", null);
                    Session.Add("page_QLCTDonHang_dataCT", null);
                    Session.Add("page_QLCTDonHang_dataSP", null);

                    Bind_Data();
                }

                sw.closeConnection();
            }
        }

        private void getData_Add()
        {
            string MaSanPham = Request.Params["MaDonHang"];
            if (MaSanPham != null && MaSanPham.Trim() != "")
            {
                IServerWeb sw = new ServerWeb();
                DonHang_DAO daoDH = new DonHang_DAO(sw.Connection);
                DonHang dh = daoDH.getByKey("DH" + MaSanPham);
                if (dh != null)
                {
                    ChiTietDonHang_DAO daoCT = new ChiTietDonHang_DAO(sw.Connection);
                    List<ChiTietDonHang> dataCT = daoCT.getListByKey(dh.MaDonHang);
                    List<SanPham> dataSP = new List<SanPham>();
                    dataCT.Add(new ChiTietDonHang()
                    {
                        Gia = 0,
                        GiamGia = 0,
                        IsStatus = 0,
                        MaDonHang = dh.MaDonHang,
                        MaSanPham = "SP",
                        SoLuong = 0
                    });
                    for (int i = 0; i < dataCT.Count; i++)
                    {
                        dataCT[i].GiamGia = dataCT[i].Gia * dataCT[i].SoLuong;
                        //SanPham_DAO daoSP = new SanPham_DAO(sw.Connection);
                        //SanPham sp = daoSP.getByKey(dataCT[i].MaSanPham);
                        //if (sp != null)
                        //{
                        //    dataSP.Add(sp);
                        //}
                        //else
                        //{
                        //    sp = new SanPham() { TenSanPham = "" };
                        //    dataSP.Add(sp);
                        //}
                    }

                    Session.Add("page_QLCTDonHang_dataDH", dh);
                    Session.Add("page_QLCTDonHang_dataCT", dataCT);
                    Session.Add("page_QLCTDonHang_dataSP", dataSP);
                    data_GridView.SelectedIndex = dataCT.Count - 1;
                    data_GridView.EditIndex = dataCT.Count - 1;
                    //((LinkButton)data_GridView.Rows[6].Cells[0].Controls[0]).Text = "Thêm";
                    
                    Bind_Data();

                    ((LinkButton)data_GridView.Rows[dataCT.Count - 1].Cells[5].Controls[0]).Text = "Thêm";
                    //CachDung.Text = sp.CachDung;
                    ////ThanhPhan.Text = sp.ThanhPhan;
                    //CongDung.Text = sp.CongDung;
                }
                else
                {
                    Session.Add("page_QLCTDonHang_dataDH", null);
                    Session.Add("page_QLCTDonHang_dataCT", null);
                    Session.Add("page_QLCTDonHang_dataSP", null);

                    Bind_Data();
                }

                sw.closeConnection();
            }
        }

        private bool checkDefaultSubmit()
        {
            if (Request.Params["btn_Tim"] != null )
            {
                return false;
            }
            return true;
        }

        protected void data_GridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            if (((LinkButton)data_GridView.Rows[e.RowIndex].Cells[5].Controls[0]).Text == "Thêm")
            {
                List<DTO.ChiTietDonHang> list = (List<DTO.ChiTietDonHang>)(Session["page_QLCTDonHang_dataCT"]);
                list.RemoveAt(e.RowIndex);
            }
            data_GridView.EditIndex = -1;
            Bind_Data();
        }

        private void Bind_Data()
        {
            data_GridView.DataSource = Session["page_QLCTDonHang_dataCT"];
            data_GridView.DataBind();
        }

        protected void data_GridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row = data_GridView.Rows[e.RowIndex];
            IServerWeb sw = new ServerWeb();
            ChiTietDonHang_DAO daoCT = new ChiTietDonHang_DAO(sw.Connection);
            List<DTO.ChiTietDonHang> list = (List<DTO.ChiTietDonHang>)(Session["page_QLCTDonHang_dataCT"]);

            daoCT.deleteByKey(list[row.DataItemIndex].MaDonHang, list[row.DataItemIndex].MaSanPham);
            
            list.RemoveAt(row.DataItemIndex);

            Bind_Data();
        }

        protected void data_GridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            data_GridView.EditIndex = e.NewEditIndex;
            Bind_Data();
        }

        protected void data_GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                
                GridViewRow row = data_GridView.Rows[e.RowIndex];
                IServerWeb sw = new ServerWeb();
                ChiTietDonHang_DAO daoCT = new ChiTietDonHang_DAO(sw.Connection);
                List<DTO.ChiTietDonHang> list = (List<DTO.ChiTietDonHang>)(Session["page_QLCTDonHang_dataCT"]);
                string maSP = list[row.DataItemIndex].MaSanPham;
                list[row.DataItemIndex].MaSanPham = Function.Func.beginConvertDataType(((TextBox)(row.Cells[1].Controls[0])).Text.Trim(), pDataType.String);
                list[row.DataItemIndex].SoLuong = float.Parse(Function.Func.beginConvertDataType(((TextBox)(row.Cells[2].Controls[0])).Text, pDataType.Decime));
                list[row.DataItemIndex].Gia = double.Parse(Function.Func.beginConvertDataType(((TextBox)(row.Cells[3].Controls[0])).Text, pDataType.Decime));
                list[row.DataItemIndex].GiamGia = list[row.DataItemIndex].SoLuong * list[row.DataItemIndex].Gia;
                list[row.DataItemIndex].IsStatus = 0;
                //list[row.DataItemIndex].GiamGia = int.Parse(Function.Func.beginConvertDataType(((TextBox)(row.Cells[5].Controls[0])).Text, pDataType.Integer));

                //listPayment[row.DataItemIndex].updateBy(listPayment[row.DataItemIndex].PaymentID.ToString());
                if (((LinkButton)data_GridView.Rows[e.RowIndex].Cells[5].Controls[0]).Text == "Thêm")
                {
                    list[row.DataItemIndex].GiamGia = 0;
                    daoCT.add(list[row.DataItemIndex]);
                    list[row.DataItemIndex].GiamGia = list[row.DataItemIndex].SoLuong * list[row.DataItemIndex].Gia;
                }
                else
                {
                    daoCT.updateByKey(list[row.DataItemIndex], list[row.DataItemIndex].MaDonHang, maSP);
                }
                data_GridView.EditIndex = -1;
                //getData();
                Bind_Data();
            }
        }

    }
}